<?php
// +----------------------------------------------------------------------
// | Author: Stupboy King
// +----------------------------------------------------------------------
namespace app\api\controller\driver;
use think\Controller;
use think\Db;
use app\api\logic;
use app\api\model\Driver as DriverModel;
/**
 * @name【登入管理】
 * @baseurl【driver.index】
 * @file【driver/Login.php】
 */
class Login extends Base
{
    protected $beforeActionList = [];
    /**
     * @name【司机登入】
     * @desc【司机登入】
     * @uri【login】
     * @method【post】
     * @param【
     * phone $ string $ 13999999999 $ 手机号 $ 1#
     * password $ string $ 8888 $ 密码 $ 1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息#
     * token $ string $ $ token $ 32位左右字符 #
     * data $ json $ $ 数据 $ {"driver_id":806,"user_id":37,"name":"测试-金司机","phone":"18626345740","head_img":null,"work_state":3,"lon":"118.180896","lat":"24.508844","update_time":"2018-03-19 10:41:01","car_id":0,"star":null,"score":null,"rate":null,"activeTime":1521526929,"loginTime":1521526929,"gps_time":1521526929,"area_id":4,"car_type_id":0,"cars":[],"other_type":""}
     * 】
     * @throws \app\base\exception\RecordNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function login(){
        if(!$this->requestMethod){
            return $this->retError(103);
        }
        $userName  = input('phone','');
        $password  = input('password','');
        $login     = new logic\Login();
        list(,$user) = $login->validateLogin($userName,$password);
        $driver = $login->getDriverLoginInfo($user['user_id']);
        $key                  = $this->appPrefix.$this->redisPreFix.$user['area_id'].'.login';
        $field                = md5($driver['user_id']);
        $nowTime              = time();
        $timeStamp            = $nowTime.$user['area_id'];
        $driver['activeTime'] = $nowTime;
        $driver['loginTime']  = $nowTime;
        $driver['gps_time']   = $nowTime;
        $driver['area_id']    = $user['area_id'];
        $this->redis->hset($key,$field,serialize($driver));
        $data['dtoken']       = $field.$timeStamp;
        $data['data']         = $driver;
        $string = 'driver_id|司机id,user_id|账号id,name|司机姓名,phone|司机电话,head_img|头像地址';
        $string.= ',work_state|司机状态,lon|当前经度,lat|当前纬度,update_time|更新时间,car_id|默认车辆';
        $string.= ',score|评分,rate|接单率,default_car_type|默认车型,default_prop|默认车属性,car_number|默认车牌';
        $string.= ',car_type_id|默认车型id,cars|绑定车辆id,other_type|绑定其他车型|车型id-车辆id';
        $string.= ',car_types|绑定车型对应车辆id|出现类型-车型:车辆id,prop|绑定车属性对应车辆id|属性id:车辆id';
        $string.= ',numbers|绑定车牌,activeTime|最新请求时间,loginTime|登入时间,gps_time|最近定位时间,area_id|区域id';
        $data['description'] = analyzeDesc($string);
        return $this->retSucc($data);
    }
    
    /**
     * @name【司机退出登入】
     * @desc【司机退出登入】
     * @uri【logout】
     * @method【logout】
     * @param【
     * dtoken $ string $ abcd $ 校验码 $ 1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息
     * 】
     */
    public function logout(){
        $this->redis->hdel($this->key,$this->token);
        return $this->retSucc();
    }

    /**
     * @name【修改密码】
     * @desc【修改密码】
     * @uri【changePassword】
     * @method【changePassword】
     * @param【
     * dtoken $ string $ abcd $ 校验码 $ 1#
     * new_password $ string $ 8888 $ 新密码 $ 1#
     * old_password $ string $ 8888 $ 旧密码 $ 1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息
     * 】
     */
    public function changePassword(){
        $this->auth();
        $new_password  = input('new_password');
        $old_password  = input('old_password');
        if(!$new_password || !$old_password){
            return $this->retError(108);
        }
        if($new_password==$old_password){
            return $this->retError(106);
        }
        $ret = DriverModel::changePassword($this->driverInfo['driver_id'],$old_password,$new_password);
        return $ret>0 ? $this->retSucc() : $this->retError(107);
    }
}